package org.glow.changgou.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.user.dao.AddressMapper;
import org.glow.changgou.user.pojo.Address;
import org.glow.changgou.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:admin
 * @Description:Address业务层接口实现类
 * @Date 2022/10/02 20:26:13
 *****/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Resource
    private AddressMapper addressMapper;


    @Override
    public List<Address> list(String username) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUsername,username);
    return     addressMapper.selectList(queryWrapper);
    }

    /**
     * Address条件+分页查询
     * @param address 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Address> findPage(Address address, int page, int size){
        //分页
        Page<Address> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,address);
        return addressMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * Address分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Address> findPage(int page, int size){
        Page<Address>pageInfo = new Page<>(page,size);
        return addressMapper.selectPage(pageInfo,null);

    }

    /**
     * Address条件查询
     * @param address
     * @return
     */
    @Override
    public List<Address> findList(Address address){
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,address);
       return addressMapper.selectList(queryWrapper);
    }



    public LambdaQueryWrapper<Address> getCondition(LambdaQueryWrapper<Address> queryWrapper,Address address){

        if(address!=null){
            // 
            if(!StringUtils.isEmpty(address.getId())){
//                    criteria.andEqualTo("id",address.getId());
                    queryWrapper.eq(Address::getId,address.getId());
            }
            // 用户名
            if(!StringUtils.isEmpty(address.getUsername())){
//                    criteria.andLike("username","%"+address.getUsername()+"%");
                    queryWrapper.eq(Address::getUsername,address.getUsername());
            }
            // 省
            if(!StringUtils.isEmpty(address.getProvinceid())){
//                    criteria.andEqualTo("provinceid",address.getProvinceid());
                    queryWrapper.eq(Address::getProvinceid,address.getProvinceid());
            }
            // 市
            if(!StringUtils.isEmpty(address.getCityid())){
//                    criteria.andEqualTo("cityid",address.getCityid());
                queryWrapper.eq(Address::getCityid,address.getCityid());
            }
            // 县/区
            if(!StringUtils.isEmpty(address.getAreaid())){
//                    criteria.andEqualTo("areaid",address.getAreaid());
                    queryWrapper.eq(Address::getAreaid,address.getAreaid());
            }
            // 电话
            if(!StringUtils.isEmpty(address.getPhone())){
//                    criteria.andEqualTo("phone",address.getPhone());
                    queryWrapper.eq(Address::getPhone,address.getPhone());
            }
            // 详细地址
            if(!StringUtils.isEmpty(address.getAddress())){
//                    criteria.andEqualTo("address",address.getAddress());
                    queryWrapper.eq(Address::getAddress,address.getAddress());
            }
            // 联系人
            if(!StringUtils.isEmpty(address.getContact())){
//                    criteria.andEqualTo("contact",address.getContact());
                    queryWrapper.eq(Address::getContact,address.getContact());
            }
            // 是否是默认 1默认 0否
            if(!StringUtils.isEmpty(address.getIsDefault())){
//                    criteria.andEqualTo("isDefault",address.getIsDefault());
                queryWrapper.eq(Address::getIsDefault,address.getIsDefault());
            }
            // 别名
            if(!StringUtils.isEmpty(address.getAlias())){
//                    criteria.andEqualTo("alias",address.getAlias());
                    queryWrapper.eq(Address::getAlias,address.getAlias());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        addressMapper.deleteById(id);
    }

    /**
     * 修改Address
     * @param address
     */
    @Override
    public void update(Address address){
        addressMapper.updateById(address);
    }

    /**
     * 增加Address
     * @param address
     */
    @Override
    public void add(Address address){
        addressMapper.insert(address);
    }

    /**
     * 根据ID查询Address
     * @param id
     * @return
     */
    @Override
    public Address findById(Integer id){
        return  addressMapper.selectById(id);
    }

    /**
     * 查询Address全部数据
     * @return
     */
    @Override
    public List<Address> findAll() {
        return addressMapper.selectList(null);
    }
}
